package command.client;

import client.ClientSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Conversation;
import model.MessageCommunication;

public class AskTheConversationHistoryClient implements CommandClient {

    public AskTheConversationHistoryClient() {
    }

    @Override
    public void execute(ClientSession clientSession, boolean isInvokedByTheServer) {
        try {
            if (isInvokedByTheServer) {
                ArrayList<Conversation> conversations = new ArrayList<>();
                conversations = (ArrayList<Conversation>) clientSession.getObjectInputStream().readObject();
                clientSession.getGUIChatPrincipal().setConversationsHistory(conversations);
            } else {
                clientSession.getObjectOutputStream().writeObject(
                        new MessageCommunication(true, "asktheconversationhistoryserver"));
                clientSession.getObjectOutputStream().flush();
                clientSession.getObjectOutputStream().writeObject(clientSession.getUserSession());
                clientSession.getObjectOutputStream().flush();
            }
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(AskTheConversationHistoryClient.class.getName()).log(Level.SEVERE,
                    "Fallo al realizar el comando de petición del historial de conversaciones en el cliente", ex);
        }
    }
}
